package com.example.officialdocumentsys.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.officialdocumentsys.annotation.LogAnno;
import com.example.officialdocumentsys.common.R;
import com.example.officialdocumentsys.common.ThreadLocalContext;
import com.example.officialdocumentsys.po.File;
import com.example.officialdocumentsys.po.Filesend;
import com.example.officialdocumentsys.service.FileService;
import com.example.officialdocumentsys.service.FilesendService;
import com.example.officialdocumentsys.vo.FileSendVO;
import com.example.officialdocumentsys.vo.FileVO;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * <p>
 * 文件收发 前端控制器
 * </p>
 *
 * @author hi
 * @since 2022-11-24
 */
@RestController
@RequestMapping("/filesend")
public class FilesendController extends BaseController<Filesend, FilesendService>{

    @LogAnno(operateType = "发送文件")
    @PostMapping("/send")
    public R<String> send(@RequestBody Filesend e){
        e.setFromUser(ThreadLocalContext.getCurrentId());
        e.setStatus(0);
        e.setSendTime(LocalDateTime.now());
        baseService.updateById(e);
        return R.success("成功");
    }

    @LogAnno(operateType = "接受文件")
    @GetMapping("/accept")
    public R<String> accept(Long id){
        Filesend e = new Filesend();
        e.setId(id);
        e.setStatus(1);
        e.setSolveTime(LocalDateTime.now());
        baseService.updateById(e);
        return R.success("已接受");
    }

    @LogAnno(operateType = "拒绝文件")
    @GetMapping("/unAccept")
    public R<String> unAccept(Long id){
        Filesend e = new Filesend();
        e.setId(id);
        e.setSolveTime(LocalDateTime.now());
        e.setStatus(2);
        baseService.updateById(e);
        return R.success("已拒绝");
    }


    @LogAnno(operateType = "浏览文件接收")
    @GetMapping("/page")
    public R<Page> list(int page, int pageSize, String name) {
        Page<FileSendVO> pageInfo = new Page(page, pageSize);
        QueryWrapper<FileSendVO> qw = new QueryWrapper<>();
        return R.success(baseService.getVO(pageInfo,qw));
    }

    @LogAnno(operateType = "用户浏览文件接收")
    @GetMapping("/pageSelf")
    public R<Page> pageSelf(int page, int pageSize,Integer way, String name,Integer level,Long fileType) {
        Page<FileSendVO> pageInfo = new Page(page, pageSize);
        QueryWrapper<FileSendVO> qw = new QueryWrapper<>();
        Long currentId = ThreadLocalContext.getCurrentId();
        qw.lambda().eq(Filesend::getFromUser,currentId).or().eq(Filesend::getToUser,currentId);
        //判断文件来源查询条件
        if (way == 2){
            qw.lambda().eq(Filesend::getFromUser, currentId);
        }else {
            qw.lambda().eq(Filesend::getToUser,currentId);
        }
        qw.eq(fileType != null,"q.file_type",fileType);
        qw.eq(level != null,"q.level",level);
        qw.eq(name != null,"q.from_user_name",name).or().eq(name != null,"q.to_user_name",name);
        return R.success(baseService.getVO(pageInfo,qw));
    }


    @LogAnno(operateType = "管理员浏览文件接收")
    @GetMapping("/pagePlus")
    public R<Page> pagePlus(int page, int pageSize,Integer way, String name,Integer level,Long typeId) {
        Page<FileSendVO> pageInfo = new Page(page, pageSize);
        QueryWrapper<FileSendVO> qw = new QueryWrapper<>();
        Long currentId = ThreadLocalContext.getCurrentId();
        qw.eq(typeId != null,"q.type_id",typeId);
        qw.eq(level != null,"q.level",level);
        qw.like(StringUtils.isNotBlank(name),"q.from_user_name",name).or().like(name != null,"q.to_user_name",name);
        return R.success(baseService.getVO(pageInfo,qw));
    }

    @PostMapping
    public R<String> save(@RequestBody Filesend e) {
        Long currentId = ThreadLocalContext.getCurrentId();
        if (e.getToUser() == currentId){
            return R.error("请勿发送给自己");
        }
        e.setFromUser(currentId);
        e.setSendTime(LocalDateTime.now());
        e.setStatus(0);
        LambdaQueryWrapper<Filesend> qw = new LambdaQueryWrapper<>();
        qw.eq(Filesend::getToUser,e.getToUser()).eq(Filesend::getFromUser,currentId).eq(Filesend::getFile,e.getFile());
        int count = baseService.count(qw);
        if (count > 0){
            return R.error("请勿重复发送");
        }

        baseService.save(e);
        return R.success("保存成功");
    }


}

